from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import BIGINT, Column, String, TEXT

Base = declarative_base()  # 生成orm基类


class To_json():
    """实体类转json工具类"""

    def to_json(self):
        """将实例对象转化为json"""
        item = self.__dict__
        if "_sa_instance_state" in item:
            del item["_sa_instance_state"]
        return item


'''登录用户相关'''


class User(Base):
    '''用户表'''
    __tablename__ = "user_table"  # 表名
    id = Column(BIGINT, primary_key=True, autoincrement=True)  # 设置主键
    userName = Column(String(50), comment="用户名", unique=True)
    password = Column(String(50), comment="密码")
    email = Column(String(50), comment="邮箱")
    name = Column(String(100), comment="姓名")
    unit = Column(String(150), comment="所在单位")

    isAdmin = Column(String(2), comment="是否是管理员")


class Participle(Base):  # 继承生成的orm基类
    ''' 分词表 '''
    __tablename__ = "participle_table"  # 表名
    id = Column(BIGINT, primary_key=True, autoincrement=True)  # 设置主键
    spell = Column(String(50), comment="拼音")
    ccte = Column(String(100), comment="汉语")
    type = Column(String(50), comment="类型")
    isdel = Column(String(100), comment="是否存在")

    def init(self, spel, cct, type):
        self.spell, self.ccte, self.type = spel, cct, type

    def to_dict(self):
        # model_dict = dict(self.__dict__)
        # del model_dict['isdel']
        # del model_dict['id']
        model_dict = {}
        model_dict['spell'] = self.spell
        model_dict['ccte'] = self.ccte
        return model_dict


class Prescription(Base):  # 继承生成的orm基类
    ''' 方剂表 '''
    __tablename__ = "prescription_table"  # 表名
    id = Column(BIGINT, primary_key=True, autoincrement=True)  # 设置主键
    name = Column(String(100), comment="方剂名称", unique=True)
    ancient = Column(TEXT, comment="古方")
    golden = Column(TEXT, comment="金方")
    song = Column(TEXT, comment="方歌")
    usage = Column(TEXT, comment="用法")
    function = Column(TEXT, comment="功用")
    major = Column(TEXT, comment="主治")
    explain = Column(TEXT, comment="方解")
    DialecticalPoint = Column(TEXT, comment="辩证要点")
    AASchange = Column(TEXT, comment="加减变化")
    application = Column(TEXT, comment="现代应用")
    attention = Column(TEXT, comment="注意")
    isExists = Column(String(4), comment="是否删除")

    def init(self, *pars):
        print(pars)
        self.name, self.ancient, self.golden, self.song, self.usage, self.function, self.major, self.explain, self.DialecticalPoint, self.AASchange, self.application, self.attention = pars

    def show(self):
        return self.id, self.name, self.ancient, self.golden, self.song, self.usage, self.function, self.major, self.explain, self.DialecticalPoint, self.AASchange, self.application, self.attention

    def get_model_dict(model):
        return dict((column.name, getattr(model, column.name))
                    for column in model.__table__.columns)


class symptomType(Base):
    '''症状类型表'''
    __tablename__ = "symptomType_table"  # 表名
    id = Column(BIGINT, primary_key=True, autoincrement=True)  # 设置主键
    name = Column(String(100), comment="症状类型", unique=True)
    isExists = Column(String(4), default=1, comment="是否删除")

    def init(self, name):
        self.name = name

    def show(self):
        return self.name


class symptomDetails(Base):
    '''症状详情'''
    __tablename__ = "symptomDetails_table"  # 表名
    id = Column(BIGINT, primary_key=True, autoincrement=True)  # 设置主键
    sid = Column(BIGINT, comment="症状类型id")
    name = Column(String(100), comment="症状的名称", unique=True)
    main = Column(String(255), comment="主症")
    therapy = Column(String(255), comment="治法")
    prescription = Column(String(255), comment="方剂")
    addSub = Column(String(255), comment="加减")
    isExists = Column(String(4), comment="是否删除")

    def init(self, *pars):
        self.name, self.main, self.therapy, self.prescription, self.addSub, self.type = pars


class Book(Base, To_json):
    """书籍类"""
    __tablename__ = "book_table"
    id = Column(BIGINT, primary_key=True, autoincrement=True)
    boot_name = Column(String(100), comment="书名", unique=True)

    def init(self, *pars):
        self.boot_name = pars

    def show(self):
        return self.id, self.boot_name


class Chapter(Base, To_json):
    """章节类"""
    __tablename__ = "chapter_table"
    id = Column(BIGINT, primary_key=True, autoincrement=True)
    book_id = Column(BIGINT, comment="书籍id")
    chapter_name = Column(String(100), comment="章节名称")

    def init(self, *pars):
        self.book_id, self.chapter_name = pars

    def show(self):
        return self.id, self.book_id, self.chapter_name


class Content(Base, To_json):
    """内容类"""
    __tablename__ = "content_table"
    id = Column(BIGINT, primary_key=True, autoincrement=True)
    chapter_id = Column(BIGINT, comment="章节id")
    content_name = Column(String(100), comment="内容名称")
    content_txt = Column(TEXT, comment="内容实体")

    def init(self, *pars):
        self.chapter_id, self.content_name, self.content_txt = pars

    def show(self):
        return self.id, self.chapter_id, self.content_name, self.content_txt


"""病症"""


class Symptom(Base, To_json):
    __tablename__ = "symptom"
    id = Column(BIGINT, primary_key=True, autoincrement=True)
    symptom_name = Column(String(255), comment="病症名称")

    def init(self, *pars):
        self.symptom_name = pars

    def update(self, *pars):
        self.id, self.symptom_name = pars


"""详细治疗方法"""


class Treat(Base, To_json):
    __tablename__ = "treat"
    id = Column(BIGINT, primary_key=True, autoincrement=True)
    symptom_id = Column(BIGINT, comment='病症id')
    symptom_name = Column(String(255), comment="病症名称")
    treat_main = Column(String(255), comment="主症")
    treat_method = Column(String(100), comment="治法")
    treat_drug = Column(String(100), comment="方剂")
    treat_add_sub = Column(String(255), comment="加减")
    treat_type = Column(String(100), comment="类型")

    def init(self, *args):
        self.symptom_id, self.symptom_name, self.treat_main, self.treat_method, self.treat_drug, self.treat_add_sub, self.treat_type = args

    def update(self, *args):
        self.id, self.symptom_id, self.symptom_name, self.treat_main, self.treat_method, self.treat_drug, self.treat_add_sub, self.treat_type = args


class commonDiseases(Base):
    '''常见疾病'''
    __tablename__ = "commonDiseases_table"
    id = Column(BIGINT, primary_key=True, autoincrement=True)
    diseasesName = Column(String(100), comment="疾病类型名称")


class commonDiseasesSymptom(Base):
    '''常见疾病症状'''
    __tablename__ = "commonDiseasesSymptom_table"
    id = Column(BIGINT, primary_key=True, autoincrement=True)
    cid = Column(BIGINT, comment="常见疾病 id")
    diseasesName = Column(String(100), comment="疾病症状")
    primarySymptom = Column(TEXT, comment="主证")
    therapy = Column(TEXT, comment="治法")
    prescription = Column(String(255), comment="方剂名称")
    empirical = Column(TEXT, comment="经验方")


if __name__ == '__main__':
    import utils

    Base.metadata.create_all(utils.engine)  # 创建表结构
# 父类Base调用所有继承他的子类来创建表结构
